Day 1 (Foundational Large Language Models & Prompt Engineering)
from 5-Day Gen AI Intensive
Today you’ll explore the evolution of LLMs, from transformers to techniques like fine-tuning and inference acceleration. You’ll also get trained in the art of prompt engineering for optimal LLM interaction.
The code lab will walk you through getting started with the Gemini API and cover several prompt techniques and how different parameters impact the prompts.
1. Complete the Intro Unit: “Foundational Large Language Models”, which is:
1. Listen to the summary podcast episode for this unit (created by NotebookLM).
https://youtu.be/mQDlCZZsOyo
2. Read the “Foundational Large Language Models & Text Generation” whitepaper.
2. Complete Unit 1 – “Prompt Engineering”, which is:
1. Listen to the summary podcast episode for this unit (created by NotebookLM).
https://youtu.be/F_hJ2Ey4BNc
2. Read the “Prompt Engineering (kaggle)” whitepaper.
半分くらい読んでるtakker.icon
意外と知らないことがあって興味深い
あと/work4aiにないワードや、説明が少ないワードが、Foundational Large Language Models & Text Generationも含めてかなりあった
おそらく当たり前過ぎて説明がないんだと思う
2週目くらいに自分の知識を整理するときにでも、/work4aiにページを生やしていきたい
3. Complete this code lab on Kaggle where you’ll learn prompting fundamentals.
Make sure you phone verify your account before starting, it's necessary for the code labs.
code labをcopy & editしたtakker.icon
https://www.kaggle.com/code/takker99/day-1-prompting/edit
編集と実行ができるようになった
https://gyazo.com/6d6c1cc6147e48a007c343f8dcfb5555
Google AI StudioでAPI keyを入手して、kaggleのnotebookのsecret保存する
発行した
https://gyazo.com/81f14c2b228dad7edfd1041ece1fc995
保存すると、このようにcode snippetが出てくる
https://gyazo.com/04ca9d50b0e6943ebcd096f4d02a2d9e
下にあったコードブロックを実行してみた
エラーになった。
前の方にあったコードブロックを飛ばして実行したから、そりゃそうなる
https://gyazo.com/e0ec8c85d125ac4877a819b746c1aa2d
多分UIはJupyter Notebookなんだろうけど、いかんせんnotebookをほとんど使ったことがないので、うまい使い方がよくわからないtakker.icon
以前のblocksを全部順に実行するコマンドRun > Run beforeがあった。これを使う
キーボードショートカットはなかった。
正常に実行された!
Run your first prompt
defaultでGemini 1.5 Flash使うように書かれている
うごいた!
https://gyazo.com/4565f5cc1df554c8d5b52acca4ab17e2
すこしいじってみる
code:py
flash = genai.GenerativeModel('gemini-1.5-flash')
response = flash.generate_content("Explain AI to me like I'm a kid in Japanese.")
print(response.text)
日本語で話してくれた
なんか謎のローマ字起こしも加わってるが……まあいいや
code:output
ねえ、知ってる? AIって言うのがいるんだよ! AIはね、まるで賢いお友達みたいなんだ。
(Nee, shitteiru? AI tte iu no ga iru nda yo! AI wa ne, marude kashikoi o-tomodachi mitai nan da.) (Hey, do you know? There's something called AI! AI is like a smart friend.)
例えばね、AIは「猫の写真を見せて!」って言ったら、猫の写真をいっぱい見せてくれるんだよ。 自分で考えて、選んで見せてくれるんだ! すごいよね?
そうなんだ!すご~い!takker.icon
code:output
(Tatteiba ne, AI wa "neko no shashin o misete!" tte ittara, neko no shashin o ippai misete kureru nda yo. Jibun de kangaete, erande misete kureru nda! Sugoi yo ne?) (For example, if you tell AI "Show me pictures of cats!", it shows you lots of cat pictures. It thinks and chooses them by itself! Amazing, right?)
あとね、AIはゲームも上手だよ! 難しいゲームでも、たくさん練習して上手になるんだ。 まるで人間みたい!
(Ato ne, AI wa ge-mu mo jouzu da yo! Muzukashii ge-mu demo, takusan renshuu shite jouzu ni naru nda. Marude ningen mitai!) (Also, AI is good at games! Even difficult games, it gets good at them by practicing a lot. Just like a human!)
でもね、AIはまだ赤ちゃんみたいなところもあるんだ。 全部が全部、完璧にできるわけじゃないんだよ。 だから、人間がAIに教えてあげたり、手伝ってあげたりすることもあるんだよ。
(Demo ne, AI wa mada akachan mitai na tokoro mo aru nda. Zenbu ga zenbu, kanpeki ni dekiru wake ja nai nda yo. Dakara, ningen ga AI ni oshiete age tari, tasukete age tari suru koto mo aru nda yo.) (But, AI is still like a baby in some ways. It can't do everything perfectly. So, humans sometimes teach and help AI.)
AIはこれからもっともっと賢くなっていくと思うよ! 楽しみだね!
(AI wa kore kara motto motto kashikoku natte iku to omou yo! Tanoshimi da ne!) (I think AI will become even smarter from now on! I'm looking forward to it!)
うん!楽しみ!takker.icon
Start a chat
multi-turnなコードの例
Temperatureが0でないから、出力にランダム性がある
https://gyazo.com/b4dc24138f7181a7301f3ca9cdd5d799
ところでKaggleのnotebookってvscodeで動かせないかなtakker.icon
多分拡張機能があるはず
ないみたい
https://zenn.dev/yuto_mo/articles/5c5311a83892b2
remote SSHを頑張る必要がある
今のところweb browserでやっても困らないから、このままweb browserでやろう
あとpythonではなくTypeScriptで動かしたい
無理そうtakker.icon
pythonかRしか対応してない
もちろんgeminiのtypescript API clientはある
https://github.com/google-gemini/generative-ai-js
Choose a model
genai.list_models()で利用可能なmodelsを取得できる
Gemini 2.0 Flashも使えるようなので、そっちに変えてみたtakker.icon
出力の文体がちょっと変わった
Explore generation parameters
output length
max_output_tokensでLLMの出力サイズを制限できる
制限を超えると、出力が途中で打ち切られる
https://gyazo.com/f1c635d554c7a04daac025e801ddca84
Temperature
429 Too Many Requestsを回避するために、試行回数を減らしてくれるmethodがあるようだ
code:py
# When running lots of queries, it's a good practice to use a retry policy so your code
# automatically retries when hitting Resource Exhausted (quota limit) errors.
retry_policy = {
"retry": retry.Retry(predicate=retry.if_transient_error, initial=10, multiplier=1.5, timeout=300)
}
temperature=0.0だとおんなじ回答ばかり返ってくることを確認した
Top-K and top-P
Top-K samplingとTop-P samplingを試す
正直違いはまだ実感できていないtakker.icon
とりあえず、ランダム性を高めるのに、Temperature sampling、Top-K sampling、Top-P samplingの3つをいじればいいと理解した
wogikaze.iconprivateからcopy
トークンの生起確率分布に従いランダムに選択
Top_p:上位p%のトークンから選択
Top_k:上位k個のトークンから選択
Temperture:Softmax手前のlogitの分母にかける
だいたい多様性
pとkは、絶対値か相対値かの違いくらいかなtakker.icon
Prompting
configでJSON出力などを指定できる
Code Prompting
適当に実行した
TypeScript+Gemini APIでサンプルコードを一通り試す
4. Read this case study to learn how a leading bank leveraged advanced prompt engineering and other contents discussed in assignments of day 1 to automate their financial advisory workflows, achieving significant productivity gains.
ざっと目を通した
銀行の事例
3. Watch the YouTube livestream recording.
Paige Bailey will be joined by expert speakers from Google - Mohammadamin Barekatain, Lee Boonstra, Logan Kilpatrick, Daniel Mankowitz, Majd Merey Al, Anant Nawalgaria, Aliaksei Severyn and Chuck Sugnet to discuss today's readings and code labs.
https://www.youtube.com/watch?v=kpRyiJUUFxY&list=PLqFaTIg4myu-b1PlxitQdY0UYIbys-2es&index=1&t=13s